<?php

namespace admin\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use admin\models\Post;

/**
 * Post2Search represents the model behind the search form about `admin\models\Post`.
 */
class Post2Search extends Post
{
    public function rules()
    {
        return [
            [['postid', 'listorder', 'forumid', 'datetime', 'anonymous', 'invisible', 'comments', 'follows', 'views', 'userid'], 'integer'],
            [['subject', 'username', 'tags', 'content', 'images', 'linkurl'], 'safe'],
        ];
    }

    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    public function search($params)
    {
        $query = Post::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'postid' => $this->postid,
            'listorder' => $this->listorder,
            'forumid' => $this->forumid,
            'datetime' => $this->datetime,
            'anonymous' => $this->anonymous,
            'invisible' => $this->invisible,
            'comments' => $this->comments,
            'follows' => $this->follows,
            'views' => $this->views,
            'userid' => $this->userid,
        ]);

        $query->andFilterWhere(['like', 'subject', $this->subject])
            ->andFilterWhere(['like', 'username', $this->username])
            ->andFilterWhere(['like', 'tags', $this->tags])
            ->andFilterWhere(['like', 'content', $this->content])
            ->andFilterWhere(['like', 'images', $this->images])
            ->andFilterWhere(['like', 'linkurl', $this->linkurl]);

        return $dataProvider;
    }
}
